package scala40rules;


public class Quatris extends CardSet {
	
	private CardSet myQuatris;


	public Quatris(CardSet cardSet){
		this.myQuatris = cardSet;
	}
	
	
	public boolean isValidQuatris(){
//      System.out.println("[Quatris || isValidQuatris()] isSameSymbol() "+isSameSymbol());
//      System.out.println("                              isDifferentSuit() "+isDifferentSuit());
//      System.out.println("                              isAllJokers() "+isAllJokers());
		if(isQuatris()){
            if(isSameSymbol()){
                if(isDifferentSuit()){
                    if(!isAllJokers()){
                        return true;
                    }
                }
            }
        }
        return false;
	}


	private boolean isReplaceable(Card currentCard, Card card){
		if( currentCard.isJoker()){
			return currentCard.getSuit().equalsIgnoreCase(card.getSuit());
		}
        return false;
	}


	public CardSet tryToSubstitute(Card card){
		int i=0;
		boolean found=false;
		Card cardOutput = new Card();
		
		if(this.existJoker()){
			while((i<myQuatris.size() && !found)){
				Card currentCard = myQuatris.getCard(i);
				if( this.isReplaceable(currentCard, card)) {
					cardOutput = myQuatris.removeCard(i);
					myQuatris.addCard(card);
					if(this.isValidQuatris()){
						found=true;
					}else{
						myQuatris.removeCard(3);
						myQuatris.addCard(cardOutput);
					}
				}
				i++;
			}
		}
		if(found){
			return myQuatris;
		}
		else{
			return null;
		}
	}
	
	//TODO ho aggiunto il controllo su ISJOKER ...percio' non e' stata testata del tutto
	private boolean isDifferentSuit(){
		boolean result;
		result = (
					((!myQuatris.getCard(0).getSuit().equals(myQuatris.getCard(1).getSuit())) || (myQuatris.getCard(0).isJoker()) || (myQuatris.getCard(1).isJoker()))
					&&
					((!myQuatris.getCard(1).getSuit().equals(myQuatris.getCard(2).getSuit())) || (myQuatris.getCard(1).isJoker()) || (myQuatris.getCard(2).isJoker()))
					&&
					((!myQuatris.getCard(2).getSuit().equals(myQuatris.getCard(3).getSuit())) || (myQuatris.getCard(2).isJoker()) || (myQuatris.getCard(3).isJoker()))
					&&
					((!myQuatris.getCard(0).getSuit().equals(myQuatris.getCard(3).getSuit())) || (myQuatris.getCard(0).isJoker()) || (myQuatris.getCard(3).isJoker()))
					&&
					((!myQuatris.getCard(0).getSuit().equals(myQuatris.getCard(2).getSuit())) || (myQuatris.getCard(0).isJoker()) || (myQuatris.getCard(2).isJoker()))
					&&
					((!myQuatris.getCard(1).getSuit().equals(myQuatris.getCard(3).getSuit())) || (myQuatris.getCard(1).isJoker()) || (myQuatris.getCard(3).isJoker()))
				);
//		if(result){System.out.println("[Quatris] isDifferentSuit = TRUE");}else{System.out.println("[Quatris] isDifferentSuit = FALSE");}
        return result;
	}
	
	
	private boolean isSameSymbol(){
		boolean result;
		int ov0 = myQuatris.getCard(0).getOrdinalValue();
		int ov1 = myQuatris.getCard(1).getOrdinalValue();
		int ov2 = myQuatris.getCard(2).getOrdinalValue();
		int ov3 = myQuatris.getCard(3).getOrdinalValue();
		result = (
					(ov0==ov1 || ov0==14 || ov1==14)
					&&
					(ov1==ov2 || ov1==14 || ov2==14)
					&&
					(ov2==ov3 || ov2==14 || ov3==14)
					&&
					(ov0==ov3 || ov0==14 || ov3==14)
					&&
					(ov0==ov2 || ov0==14 || ov2==14)
					&&
					(ov1==ov3 || ov1==14 || ov3==14)
				 );
//		if(result){System.out.println("[Quatris] isSameSymbol = TRUE");}else{System.out.println("[Quatris] isSameSymbol = FALSE");}
		return result;
	}
	
	
	private boolean isQuatris(){
		return (this.myQuatris.size() == 4);
	}


	public CardSet getMyQuatris() {
		return this.myQuatris;
	}


	public void setMyQuatris(CardSet myQuatris) {
		this.myQuatris = myQuatris;
	}


	private boolean isAllJokers() {
		boolean result = (
					(myQuatris.getCard(0).isJoker())
					&&
					(myQuatris.getCard(1).isJoker())
					&&
					(myQuatris.getCard(2).isJoker())
					&&
					(myQuatris.getCard(3).isJoker())
				);
//      if(result){System.out.println("[Quatris] isAllJoker = TRUE");}else{System.out.println("[Quatris] isAllJoker = FALSE");}
		return result;
	}


	public int sumQuatrisValue(){
		for(int i=0; i<4; i++){ //se le prime tre sono matte
			int j = this.myQuatris.getCard(i).getOrdinalValue();
			switch(j){
			case 1: return 44;
			case 2: 
			case 3: 
			case 4: 
			case 5: 
			case 6: 
			case 7:
			case 8:
			case 9:
			case 10: return (j*4);
			case 11:
			case 12:
			case 13: return 40;
			}
		}
		return 4000;

	}


    public int size(){
		return this.myQuatris.size();
	}


    public boolean existJoker(){
		for (int i=0; i<this.myQuatris.size(); i++){
			if(this.myQuatris.getCard(i).isJoker()){
				return true;
			}
		}
		return false;
	}

    public boolean existJokerWithoutSuit(){
		System.out.println("[Quatris || existJoker()] this.myTris.size()"+this.myQuatris.size());
		for (int i=0; i<this.myQuatris.size(); i++){
			System.out.println("                                      "+i);
			System.out.println("      [Quatris || existJoker()] Carta: "+this.myQuatris.getCard(i).getSymbol());
			if(this.myQuatris.getCard(i).isJoker() && this.myQuatris.getCard(i).getSuit().equalsIgnoreCase(" ")){
				return true;
			}
		}
		return false;
	}
    
    public int getFirstJokerWithoutSuitPosition(){
		for (int i=0; i<this.myQuatris.size(); i++){
//			System.out.println("                                      "+i);
//			System.out.println("      [CardSet || getFirstJokerPosition()] Carta: "+this.getCard(i).getSymbol());
			if(this.myQuatris.getCard(i).isJoker() && this.myQuatris.getCard(i).getSuit().equalsIgnoreCase(" ")){
				return i;
			}
		}
		return -1;
	}

    public void replaceWithSuitedJoker(){
        SuitList suitList = new SuitList();
        int i=0;
        while(i<myQuatris.size()){
            if(!myQuatris.getCard(i).isJoker()){
                suitList.remove(myQuatris.getCard(i).getSuit());
                i++;
            }
            else{
                myQuatris.removeCard(i);
            }
        }
        for(i=0; i<suitList.size(); i++){
            myQuatris.addCard(new Card(14, "Joker", suitList.getSuit(i), true, "src/cards/joker-"+suitList.getSuit(i)+".png","src/small_cards/joker-"+suitList.getSuit(i)+".png"));
        }
    }


    public void printQuatris(){
        this.printQuatris("NESSUN TITOLO");
    }


    public void printQuatris(String titolo){
        int symbolLenght = 8;
        int suitLenght = 8;
        int valueLenght = 7;
        System.out.println("\n\nprintScale: "+titolo);
        System.out.println("[ SYMBOL |  SUIT  | VALUE ]");
        System.out.println("---------------------------");
        for(int i=0; i<this.myQuatris.size(); i++){
            System.out.print("[");
            System.out.print(getSpaces((symbolLenght - this.myQuatris.getCard(i).getSymbol().length())/2));
            System.out.print(this.myQuatris.getCard(i).getSymbol());
            System.out.print(getSpaces((symbolLenght - this.myQuatris.getCard(i).getSymbol().length())/2 + (symbolLenght - this.myQuatris.getCard(i).getSymbol().length())%2));
            System.out.print("|");
            System.out.print(getSpaces((suitLenght - this.myQuatris.getCard(i).getSuit().length())/2));
            System.out.print(this.myQuatris.getCard(i).getSuit());
            System.out.print(getSpaces((suitLenght - this.myQuatris.getCard(i).getSuit().length())/2 + (suitLenght - this.myQuatris.getCard(i).getSuit().length())%2));
            System.out.print("|");
            System.out.print(getSpaces((valueLenght - Integer.toString(this.myQuatris.getCard(i).getOrdinalValue()).length())/2));
            System.out.print(this.myQuatris.getCard(i).getOrdinalValue());
            System.out.print(getSpaces((valueLenght - Integer.toString(this.myQuatris.getCard(i).getOrdinalValue()).length())/2 + (valueLenght - Integer.toString(this.myQuatris.getCard(i).getOrdinalValue()).length())%2));
            System.out.println("]");
        }
        System.out.println("\n");
    }


    
}
